import Vue from 'vue'
import Router from 'vue-router'
/////导入登录路由
import Login from './compontents/Login.vue'
import Home from './compontents/Home.vue'
import Welcome from './compontents/Welcome.vue'
import Users from './compontents/user/Users.vue'
import Rights from './power/Rights.vue'
import Roles from './power/Roles.vue'
import Test from './Test.vue'
import Categories from './goods/Categories.vue'
Vue.use(Router)
    ////////配置路由规则

const router = new Router({
        routes: [
            { path: '/', redirect: '/login' },
            { path: '/login', component: Login },
            {
                path: '/home',
                component: Home,
                redirect: '/welcome',
                children: [{
                    path: '/welcome',
                    component: Welcome,
                }, {
                    path: '/users',
                    component: Users,
                }, {
                    path: '/rights',
                    component: Rights,
                }, {
                    path: '/roles',
                    component: Roles,
                }, {
                    path: '/test',
                    component: Test,
                }, {
                    path: '/categories',
                    component: Categories,
                }]
            }
        ]
    })
    /////开启路由的导航守卫   是路由的一个方法  就是用来拦截的
router.beforeEach((to, from, next) => {
    // 如果用户直接去往login页面 就用to     
    /// next()   就是放行   
    if (to.path == '/login') return next();
    const tokenStr = window.sessionStorage.getItem('token');
    if (!tokenStr) return next('/login');
    next()
})
export default router